package com.college.controllers;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.college.services.IconNavigationServiceImpl;
import com.college.dao.StudentDAO;
import com.college.domain.Student;
import com.college.interfaces.IStudentDAO;
import com.college.interfaces.IconNavigationService;
import com.college.interfaces.MenuNavigationService;

@Controller
public class LoginController {
	
	private String name;
	private String userId;
	
	@Autowired
	private IconNavigationService iconNavigationService;
	
	@Autowired
	private MenuNavigationService menuNavigationService;
	
	@Autowired
	private IStudentDAO studentDAO;
 
	@RequestMapping(value="/home", method = RequestMethod.GET)
	public String printWelcome(ModelMap model,HttpSession session) {
 
		User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		name = user.getUsername();
		userId = name;
		session.setAttribute("userId", userId);
		Set<GrantedAuthority> roles = new TreeSet<GrantedAuthority>();
		roles = (Set<GrantedAuthority>) user.getAuthorities();
		System.out.println(roles.size());
		session.setAttribute("roleList", roles);
		for(GrantedAuthority role: roles){
			if(role.equals("ROLE_STUDENT")|| role.equals("ROLE_BATCHREP")){
				 List<Student> student = studentDAO.getStudentNameById(name);
				 if(student!=null){
					 for(Student stud:student){
					 name= stud.getFirstName();
					 }
					 
				 }
			}
		}
		
		//Getting icon navigation Map
		LinkedHashMap<String,String> iconMap = new LinkedHashMap<String,String>();
		iconMap = iconNavigationService.getIconNavigationBasedOnRole(roles);
		//Getting Menu Navigation Map
		Map<String, Map> menuMap = new TreeMap<String, Map>();
		menuMap = menuNavigationService.getMenuNavigationBasedOnRole(roles);
		int size = menuMap.size();
		session.setAttribute("menuMap", menuMap);
		model.addAttribute("iconMap", iconMap);
		session.setAttribute("username", name);
		
		model.addAttribute("message", "Spring Security login + database example");
		
		return "home";
		
	}
	


	@RequestMapping(value="/login", method = RequestMethod.GET)
	public String login(ModelMap model) {
 
		return "login";
 
	}
	
	@RequestMapping(value="/loginfailed", method = RequestMethod.GET)
	public String loginerror(ModelMap model) {
 
		model.addAttribute("error", "true");
		return "login";
 
	}
	
	@RequestMapping(value="/logout", method = RequestMethod.GET)
	public String logout(ModelMap model,HttpSession session) {
		/*session.removeAttribute("menuMap");
		session.removeAttribute("iconMap");
		session.removeAttribute("roleList");
		session.invalidate();*/
		return "login";
 
	}
	
}